package com.misboot.document.config;

import cn.ewsd.common.utils.CookieUtil;
import cn.ewsd.common.utils.StringUtils;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * feign config
 *
 * @Author 湖南佐佑时代科技有限公司
 * @Version 5.4.21
 * @Email service@zuoyo.com
 * @Date 2022-06-13 09:39:40
 */
public class FeignConfig implements RequestInterceptor {

    public FeignConfig() {

    }

    /**
     * Openfeign调用日志级别
     * NONE 不输出日志
     * BASIC 只有请求方法、URL、响应状态代码、执行时间
     * HEADERS 基本信息以及请求和响应头
     * FULL 请求和响应 的heads、body、metadata，建议使用这个级别
     *
     * @return NONE BASIC HEADERS FULL
     */
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.BASIC;
    }

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes var2 = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null != var2) {
            HttpServletRequest var3 = var2.getRequest();
            //获取Cookie中token
            String var4 = CookieUtil.getCookieByName(var3, "token");
            if (StringUtils.isNullOrEmpty(var4)) {
                //获取Header中token
                var4 = var3.getHeader("token");
            }
            if (StringUtils.isNullOrEmpty(var4)) {
                //获取Parameter中token
                var4 = var3.getParameter("token");
            }
            requestTemplate.header("token", new String[]{var4});
            requestTemplate.header("requestType", new String[]{"feign"});
        }
    }
}
